package com.tanhua.server.controller;

import cn.hutool.core.util.ObjectUtil;
import com.tanhua.common.utils.TypeEnum;
import com.tanhua.server.paramPojo.NotificationBo;
import com.tanhua.server.paramPojo.UpdateUserInfoBo;
import com.tanhua.server.service.MyCenterService;
import com.tanhua.server.vo.PageResult;
import com.tanhua.server.vo.SettingsVo;
import com.tanhua.server.vo.UserInfoVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Map;

/**
 * @author feihao
 * @version 1.0
 * @date 2021/7/5 21:33
 */

@Slf4j
@RestController
@RequestMapping("users")
public class MyCenterController {


    @Autowired
    private MyCenterService myCenterService;

    @Autowired
    RestTemplate restTemplate;

    /**
     * 根据用户id查询用户信息
     *
     * @param userId 用户id，如果为空，表示查询当前登录人的信息
     * @return
     */
    @GetMapping
    public ResponseEntity<UserInfoVo> queryUserInfoByUserId(@RequestParam(value = "userID", required = false) Long userId) {
        try {
            UserInfoVo userInfoVo = myCenterService.queryUserInfoByUserId(userId);
            if (ObjectUtil.isNotEmpty(userInfoVo)) {
                return ResponseEntity.ok(userInfoVo);
            }
        } catch (Exception e) {
            log.error("根据用户id查询用户信息出错~ userId = " + userId, e);
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }


    /**
     * 更新用户信息接口
     *
     * @param updateUserInfoBo
     * @return
     */
    @PutMapping
    public ResponseEntity<Void> updateUserInfo(@RequestBody UpdateUserInfoBo updateUserInfoBo) {

        try {
            Boolean b = myCenterService.updateUserInfo(updateUserInfoBo);
            if (b) {
                return ResponseEntity.ok(null);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }


    /**
     * 上传更新用户头像接口
     *
     * @param multipartFile
     * @param token
     * @return
     */
    @PostMapping("header")
    public ResponseEntity<Object> updateHeadPhoto(@RequestParam("headPhoto") MultipartFile multipartFile,
                                                  @RequestHeader("Authorization") String token) throws IOException {
        return myCenterService.saveHeadPhoto(multipartFile, token);
    }


    /**
     * 修改陌生人问题接口：
     *
     * @param mapParam
     * @return
     */
    @PostMapping("questions")
    public ResponseEntity<Void> saveOrUpdateQuestion(@RequestBody Map<String, Object> mapParam) {
        String content = mapParam.get("content").toString();
        try {
            Boolean b = myCenterService.updateOrSaveQuestion(content);
            if (b) {
                return ResponseEntity.ok(null);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }


    /**
     * 查询设置信息：
     *
     * @return
     */
    @GetMapping("settings")
    public ResponseEntity<SettingsVo> querySettings() {
        try {
            SettingsVo settingsVo = myCenterService.querySettings();
            if (ObjectUtil.isEmpty(settingsVo)) {
                return null;
            }
            return ResponseEntity.ok(settingsVo);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }


    /**
     * 保存通知的设置接口：
     *
     * @param notificationBo
     * @return
     */
    @PostMapping("notifications/setting")
    public ResponseEntity<Void> saveNotifications(@RequestBody NotificationBo notificationBo) {

        try {
            Boolean b = myCenterService.saveNotifications(notificationBo);
            if (b) {
                return ResponseEntity.ok(null);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }


    /**
     * 查询黑名单列表：
     *
     * @param page
     * @param pageSize
     * @return
     */

    @GetMapping("blacklist")
    public ResponseEntity<PageResult> queryBlackList(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                                     @RequestParam(value = "pagesize", defaultValue = "10") Integer pageSize) {

        try {
            PageResult pageResult = myCenterService.queryBlackList(page, pageSize);
            if (!ObjectUtil.isNotEmpty(pageResult)) {
                return null;
            }
            return ResponseEntity.ok(pageResult);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }


    /**
     * 移除黑名单：
     *
     * @param blackUserId
     * @return
     */
    @DeleteMapping("blacklist/{uid}")
    public ResponseEntity<Void> deleteBlackUser(@PathVariable("uid") Long blackUserId) {

        try {
            Boolean b = myCenterService.deleteBlackUser(blackUserId);
            if (b) {
                return ResponseEntity.ok(null);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();

    }


    /**
     * 查询粉丝，互相喜欢，喜欢的count:
     *
     * @return
     */
    @GetMapping("counts")
    public ResponseEntity<Map<String, Integer>> getCounts() {
        try {
            Map<String, Integer> mapCounts = myCenterService.getCounts();
            return ResponseEntity.ok(mapCounts);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }


    /**
     * 粉四、访客、互相喜欢、喜欢人查询接口：
     *
     * @param type
     * @param page
     * @param pageSize
     * @param nickname
     * @return
     */


    @GetMapping("friends/{type}")
    public ResponseEntity<PageResult> queryFriendsList(@PathVariable("type") Integer type,
                                                       @RequestParam(value = "page", defaultValue = "1") Integer page,
                                                       @RequestParam(value = "pagesize", defaultValue = "10") Integer pageSize,
                                                       @RequestParam(value = "nickname", required = false) String nickname) {

        //转换为枚举：
        TypeEnum typeEnum = TypeEnum.getByCodeId(type);

        try {
            PageResult pageResult = myCenterService.queryFriendList(typeEnum, page, pageSize, nickname);
            return ResponseEntity.ok(pageResult);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }


    /**
     * 删除喜欢的用户（互相喜欢，喜欢列表的）
     *
     * @param deleteUserId
     * @return
     */
    @DeleteMapping("like/{uid}")
    public ResponseEntity<Void> deleteLikeUser(@PathVariable("uid") Long deleteUserId) {

        try {
            Boolean b = myCenterService.deleteLikeUserById(deleteUserId);
            if (b) {
                return ResponseEntity.ok(null);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }


    /**
     * 喜欢粉丝：
     *
     * @return
     */
    @PostMapping("fans/{uid}")
    public ResponseEntity<Void> likeFans(@PathVariable("uid") Long likeFansId) {

        try {
            Boolean b = myCenterService.likeFansById(likeFansId);
            if (b) {
                return ResponseEntity.ok(null);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }


    /**
     * 是否喜欢接口：
     *
     * @param userId
     * @return
     */
    @GetMapping("{uid}/alreadyLove")
    public ResponseEntity<Boolean> isLike(@PathVariable("uid") Integer userId) {

        try {
            Boolean b = myCenterService.isLike(userId);
            return ResponseEntity.ok(b);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

}
